//
//  YAYIAlertView.m
//  yayi
//
//  Created by hxp on 16/3/17.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIAlertView.h"
@implementation YAYIAlertView
-(instancetype)initWithView:(UIView *)view title:(NSString *)title message:(NSString *)message canCell:(NSString *)cancell otherBtn:(NSString *)sure
{
    self = [super initWithFrame:view.frame];
    if (self) {
        _title = title;
        _message = message;
        _cancelStr = cancell;
        _sureStr = sure;
        _frame = view.window.frame;
        [self createControl];
        [self createAlerView];
        [self createViews];
        
    }
    return self;
}
-(void)createControl{
    _control = [[UIControl  alloc] initWithFrame:CGRectMake(0, 0, SCREEN_width, SCREEN_height)];
    _control.backgroundColor = [UIColor lightGrayColor];
    _control.alpha = 0.2;
    [_control addTarget:self action:@selector(removesubViews) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_control];
}
-(void)createAlerView{
    _alertView = [UIView new];
    _alertView.clipsToBounds = YES;
    _alertView.layer.cornerRadius = 3;
    _alertView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_alertView];
    [_alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(280, 100));
    }];

}
-(void)createViews
{
    _msgLabel = [UILabel new];
    _msgLabel.text = _title;
    _msgLabel.backgroundColor = [UIColor whiteColor];
    _msgLabel.font = [UIFont systemFontOfSize:16];
    _msgLabel.textAlignment = NSTextAlignmentCenter;
    _msgLabel.textColor = [UIColor colorWithRed:38/255.0 green:37/255.0 blue:37/255.0 alpha:1];
    [_alertView addSubview:_msgLabel];
    [_msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_alertView.mas_left);
        make.top.equalTo(_alertView.mas_top).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(280, 45));
    }];
    
    UILabel *line = [UILabel new];
    line.backgroundColor = [UIColor lightGrayColor];
    [_alertView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_msgLabel.mas_left);
        make.top.mas_equalTo(_alertView.mas_centerY).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(280, 0.5));
    }];
    
    _cancell = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancell.tag = 1;
    _cancell.titleLabel.font = [UIFont systemFontOfSize:font16];
    [_cancell  addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_cancell setTitle:_cancelStr forState:UIControlStateNormal];
    [_cancell setTitleColor:[UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1] forState:UIControlStateNormal];
    [_alertView addSubview:_cancell];
    [_cancell mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_alertView.mas_left);
        make.top.mas_equalTo(_msgLabel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(140 , 45));
    }];
    
    UILabel *vline = [UILabel new];
    vline.backgroundColor = [UIColor lightGrayColor];
    [_alertView addSubview:vline];
    [vline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_cancell.mas_right);
       make.top.mas_equalTo(_msgLabel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(1, 45));
    }];
    
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBtn.tag = 2;
    _sureBtn.titleLabel.font = [UIFont systemFontOfSize:font16];
    [_sureBtn  addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_sureBtn setTitle:_sureStr forState:UIControlStateNormal];
    [_sureBtn setTitleColor:[UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1] forState:UIControlStateNormal];
    [_alertView addSubview:_sureBtn];
    [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(vline.mas_right);
        make.top.mas_equalTo(_msgLabel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(140,45));
    }];
     
}
-(void)btnAction:(UIButton *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
        [_delegate alertView:self clickedButtonAtIndex:sender.tag];
        [_control removeFromSuperview];
        [_alertView removeFromSuperview];
        [self removeFromSuperview];
    }
}
-(void)removesubViews{
    [_control removeFromSuperview];
    [_alertView removeFromSuperview];
    [self removeFromSuperview];
}
@end
